package com.example.assign2_list;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TasksDataSource {

	  // Database fields
	  public SQLiteDatabase database;
	  private TasksDbHelper dbHelper;
	  public String[] allColumns = { TasksDbHelper.COLUMN_ID,
			  TasksDbHelper.COLUMN_YEAR, TasksDbHelper.COLUMN_COUNTRY };

	  public TasksDataSource(Context context) {
	    dbHelper = new TasksDbHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public TodoTask createTask(int year, String country) {
	    ContentValues values = new ContentValues();
	    values.put(TasksDbHelper.COLUMN_YEAR, year);
	    values.put(TasksDbHelper.COLUMN_COUNTRY, country);
	    long insertId = database.insert(TasksDbHelper.TASKS_TABLE_NAME, null, values);
	    Cursor cursor = database.query(TasksDbHelper.TASKS_TABLE_NAME,
	        allColumns, TasksDbHelper.COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    TodoTask newTask = cursorToTask(cursor);
	    cursor.close();
	    return newTask;
	  }

	  public void deleteTask(TodoTask task) {
	    long id = task.getId();
	    System.out.println("Task deleted with id: " + id);
	    database.delete(TasksDbHelper.TASKS_TABLE_NAME, TasksDbHelper.COLUMN_ID
	    		+ " = " + id, null);
	  }
	  
	  public TodoTask getTask(long taskId) {
		  String restrict = TasksDbHelper.COLUMN_ID + "=" + taskId;
		  Cursor cursor = database.query(true, TasksDbHelper.TASKS_TABLE_NAME, allColumns, restrict, 
		    		                        null, null, null, null, null);
		  if (cursor != null && cursor.getCount() > 0) {
			  cursor.moveToFirst();
			  TodoTask task = cursorToTask(cursor);
			  return task;
		  }
		  // Make sure to close the cursor
		  cursor.close();
		  return null;
	  }
	  
	  public boolean updateTask(long taskId, int year, String country) {
		  ContentValues args = new ContentValues();
		  args.put(TasksDbHelper.COLUMN_YEAR, year);
		  args.put(TasksDbHelper.COLUMN_COUNTRY, country);

		  String restrict = TasksDbHelper.COLUMN_ID + "=" + taskId;
		  return database.update(TasksDbHelper.TASKS_TABLE_NAME, args, restrict , null) > 0;
	  } 

	  public ArrayList<TodoTask> getAllTasks() {
	    ArrayList<TodoTask> tasks = new ArrayList<TodoTask>();

	    Cursor cursor = database.query(TasksDbHelper.TASKS_TABLE_NAME,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      TodoTask task = cursorToTask(cursor);
	      tasks.add(task);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return tasks;
	  }
	  
	  public ArrayList<TodoTask> sortBy(String column){
		  ArrayList<TodoTask> tasks = new ArrayList<TodoTask>();

		    Cursor cursor = database.query(TasksDbHelper.TASKS_TABLE_NAME,
		        allColumns, null, null, null, null, column);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		      TodoTask task = cursorToTask(cursor);
		      tasks.add(task);
		      cursor.moveToNext();
		    }
		    // Make sure to close the cursor
		    cursor.close();
		    return tasks;
	  }

	  public TodoTask cursorToTask(Cursor cursor) {
		  TodoTask task = new TodoTask();
		  task.setId(cursor.getLong(0));
		  task.setTask(cursor.getInt(1), cursor.getString(2));
		  return task;
	  }
	} 
